package qnautil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class BoardDAO {
	
	public BoardDAO(){
	}
	
	public int Write(BoardVO vo){
	/*	WriteAction에서 값을 가지고온 vo값을 저장을한다*/
		System.out.println("BoardDAOd에서 값은:"+vo);
		PreparedStatement pstmt = null;
		Connection con = null;
		String sql = "insert into board values(brd_num.NEXTVAL,?,?,?,?,sysdate)";
		int result = 0;
		try{
			con = DBUtil.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, vo.getWriter());
			System.out.println("BoardDAOd에서 값은:"+vo.getWriter());
			pstmt.setString(2, vo.getPasswd());
			System.out.println("BoardDAOd에서 값은:"+vo.getPasswd());
			pstmt.setString(3, vo.getTitle());
			System.out.println("BoardDAOd에서 값은:"+ vo.getTitle());
			pstmt.setString(4, vo.getContent());
			System.out.println("BoardDAOd에서 값은:"+ vo.getContent());
			result = pstmt.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtil.close(con, pstmt, null);
		}
		return result;
	}
	
	public int Modify(BoardVO vo){
		PreparedStatement pstmt = null;
		Connection con = null;
		String sql = "update board set writer=?, passwd=?, title=?, content=?, day=sysdate where num=?";
		int result = 0;
		try{
			con = DBUtil.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, vo.getWriter());
			pstmt.setString(2, vo.getPasswd());
			pstmt.setString(3, vo.getTitle());
			pstmt.setString(4, vo.getContent());
			pstmt.setInt(5, vo.getNum());
			result = pstmt.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtil.close(con, pstmt, null);
		}
		return result;
	}
	
	public ArrayList<BoardVO> List(){
		PreparedStatement pstmt = null;
		Connection con = null;
		String sql = "select * from board order by num desc";
		ResultSet rs = null;
		BoardVO vo = null;
		ArrayList<BoardVO> list = new ArrayList<BoardVO>();
		try{
			con = DBUtil.getConnection();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()){
				vo = new BoardVO(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6));
				list.add(vo);
			}
			
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtil.close(con, pstmt, rs);
		}
		return list;
	}

	public BoardVO Read(BoardVO vo){
		PreparedStatement pstmt = null;
		Connection con = null;
		String sql = "select * from board where num=?";
		ResultSet rs = null;
		try{
			con = DBUtil.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, vo.getNum());
			rs = pstmt.executeQuery();
			while (rs.next()){
				vo = new BoardVO(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6));
			}
			
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtil.close(con, pstmt, rs);
		}
		return vo;
	}	
	

	public int Delete(BoardVO vo){
		PreparedStatement pstmt = null;
		Connection con = null;
		String sql = "delete from board where num=?";
		int result = 0;
		try{
			con = DBUtil.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, vo.getNum());
			result = pstmt.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtil.close(con, pstmt, null);
		}
		return result;
	}
	
	public boolean passChk(BoardVO vo){
		System.out.println("BoardDAO단  패스워드 확인 단"+vo);
		PreparedStatement ptmt = null;
		Connection con= null;
		String sql="select * from board where num=? and passwd=?";
		ResultSet rs =null;
		boolean result = false;
		System.out.println("BoardDAO단  패스워드 boolean "+result);
		try{
			con = DBUtil.getConnection();
			ptmt= con.prepareStatement(sql);
			ptmt.setInt(1, vo.getNum());
			ptmt.setString(2, vo.getPasswd());
			rs = ptmt.executeQuery();	
			System.out.println("BoardDAO단 try 쪽 확인중"+rs);
			if(rs.next()){
				result = true;
			} else {
				result = false;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBUtil.close(con, ptmt, rs);
		}
		System.out.println("BoardDAO단에서 마지막단 확인중"+result);
		return result;
	
	}
	
}